﻿namespace DeltaGroup.WheelOfJeopardy.Util
{
    using System;

    public static class WheelMathUtil
    {
        #region Fields

        public const double CIRCLE_DEGREES = 360d;
        public const double CIRCLE_DIAMETER = 400d;

        #endregion Fields

        #region Methods

        /// <summary>
        /// Calculates the index of the rotation degrees to wheel sector by.
        /// </summary>
        /// <param name="sectorCount">The sector count.</param>
        /// <param name="sectorIndex">Index of the sector.</param>
        /// <param name="currentAngle">The current angle.</param>
        /// <returns></returns>
        public static double CalculateRotationDegreesToWheelSectorByIndex(int sectorCount, int sectorIndex, double currentAngle)
        {
            double sectorDegrees = CalculateWheelSectorDegrees(sectorCount);
            double sectorAngle = CIRCLE_DEGREES - (sectorDegrees * sectorIndex);
            double degreesToZero = (CIRCLE_DEGREES - (currentAngle % CIRCLE_DEGREES));
            return degreesToZero + sectorAngle - (sectorDegrees*0.5d);
        }

        /// <summary>
        /// Calculates the index of the selected sector.
        /// </summary>
        /// <param name="sectorCount">The sector count.</param>
        /// <param name="degreesFromZero">The degrees from zero.</param>
        /// <returns></returns>
        public static int CalculateSelectedSectorIndex(int sectorCount, double degreesFromZero)
        {
            double sectorDegrees = CalculateWheelSectorDegrees(sectorCount);
            double singleRotationDegrees = (Math.Floor(degreesFromZero) % CIRCLE_DEGREES);
            return (sectorCount - 1) - (int)Math.Floor(singleRotationDegrees / sectorDegrees);
        }

        /// <summary>
        /// Calculates the wheel sector degrees.
        /// </summary>
        /// <param name="sectorCount">The sector count.</param>
        /// <returns></returns>
        public static double CalculateWheelSectorDegrees(int sectorCount)
        {
            return CIRCLE_DEGREES / sectorCount;
        }

        #endregion Methods
    }
}